﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 单例动画管理者 开始找个物体挂着 切换场景不消失
/// </summary>
public class AnimationMgr : MonoBehaviour
{
    public static AnimationMgr instance = null;

    Animation anim;//声明

    private void Awake()
    {
        if (instance == null)
            instance = this;
    }

    // Start is called before the first frame update
    void Start()
    {
        //DontDestroyOnLoad(this.gameObject);//切换场景不消失
    }

    /// <summary>
    /// 哪个物体，播放的动画名，第几帧开始，第几帧结束
    /// </summary>
    /// <param name="obj带Animator组件的物体"></param>
    /// <param name="Name该物体的Animator已剪切的动画名称"></param>
    /// <param name="StartFps开始帧数"></param>
    /// <param name="EndFps结束帧数"></param>
    public void Play(GameObject obj, string Name, float StartFps, float EndFps)
    {

        anim = obj.GetComponent<Animation>();//获取Animator
        float time;//当前剪切后的总时长
        time = anim.GetClip(Name).averageDuration;
        Debug.Log("time" + time);
        float fFps;//帧率
        fFps = anim.clip.frameRate;
        Debug.Log("fFps" + fFps);
        float totalFrame;//计算动画片段总帧数
        totalFrame = time / (1 / fFps);
        Debug.Log("totalFrame" + totalFrame);
        float StartFpsTime;//当前帧率是第几秒开始播放
        StartFpsTime = StartFps / fFps;
        Debug.Log("StartFpsTime" + StartFpsTime);
        anim.Stop();//先停止在播放
        anim.Play(Name);//播放剪切好的动作写入名字
        anim[Name].time = StartFpsTime;//从第几秒开始播放
        float EndFpsTime;//当前帧率是第几秒停止
        EndFpsTime = EndFps / fFps;
        Debug.Log("EndFpsTime" + EndFpsTime);
        float Endtime;//第几秒停止
        Endtime = EndFpsTime - StartFpsTime;
        Debug.Log("Endtime" + Endtime);
        Invoke("EndFpsStop", Endtime);

    }
    /// <summary>
    /// 携程来控制停止
    /// </summary>
    /// <returns></returns>
    void EndFpsStop()
    {
        anim.Stop();//停止
    }

    // Update is called once per frame
    //void Update()
    //{
    //    if (Input.GetKey(KeyCode.A))
    //    {
    //        //AnimationMgr.instance.Play(GameObject.Find("MKJC").gameObject, "walk", 53, 70);//播放
    //        StartCoroutine( AnimationMgr.instance.Play( "walk", GameObject.Find("MKJC").gameObject, 53, 70));//播放)
    //    }

    //    if (Input.GetKey(KeyCode.S))
    //    {
    //        //AnimationMgr.instance.Play(GameObject.Find("MKJC").gameObject, "walk", 10, 20);//播放
    //        StartCoroutine(AnimationMgr.instance.Play("walk", GameObject.Find("MKJC").gameObject, 150, 300));//播放)
    //    }

    //    if (Input.GetKey(KeyCode.D))
    //    {
    //        //AnimationMgr.instance.Play(GameObject.Find("MKJC").gameObject, "walk", 12, 14);//播放
    //        StartCoroutine(AnimationMgr.instance.Play("walk", GameObject.Find("MKJC").gameObject, 12, 120));//播放)
    //    }
    //}

    public void PlayAni(string aniName, GameObject obj, float startFrame, float endFrame)
    {
        StartCoroutine(AnimationMgr.instance.Play(aniName, obj, startFrame, endFrame));//播放)
    }
    public IEnumerator Play(string aniName, GameObject obj, float startFrame, float endFrame/*float fps,float playSpeed,bool setLoop,AudioSource 交互点音乐, float 交互点音乐开始时间, AudioSource 人物语言_Clip*/)
    {

        Animation sourceClip = obj.GetComponent<Animation>();
        //停止循环动画播放();
        if (!sourceClip[aniName])
        {
            Debug.Log("没有找到" + sourceClip.gameObject.name + "的动画");
            yield break;
        }
        //sourceClip[aniName].clip.frameRate = fps;//设置帧率

        float startTime = (startFrame / sourceClip[aniName].clip.frameRate) / sourceClip[aniName].clip.length;//开始帧除以帧率就是开始时间


        //设置循环
        //if (setLoop)
        //{
        //    sourceClip[aniName].clip.wrapMode = WrapMode.Loop;
        //    Debug.Log("循环");
        //}
        //else
        //{
        //    sourceClip[aniName].clip.wrapMode = WrapMode.Once;
        //    Debug.Log("一次");
        //}

        sourceClip[aniName].clip.wrapMode = WrapMode.Once;
        Debug.Log("播放一次");


        sourceClip[aniName].normalizedTime = startTime;
        //print(playSpeed);
        //if (playSpeed == 0)
        //    playSpeed = 1;
        sourceClip[aniName].speed = 1;
        //Debug.Log(sourceClip[aniName].speed);
        sourceClip.Play(aniName);
        float Frame = (endFrame - startFrame)/*/playSpeed*/ ;

        Debug.Log("我从第" + sourceClip[aniName].normalizedTime + "秒开始播放的");
        //播放动画时还要播放音乐，音乐从第某帧开始，最后判断音乐和动画都结束才进行下一步

        //声音播放完毕 = false;
        ////旁白声音播放完毕 = false;
        //float musicStartTime = 交互点音乐开始时间 / sourceClip[aniName].clip.frameRate/ playSpeed;
        //StartCoroutine(播放声音(交互点音乐, musicStartTime));
        //if (action.characterLanguage != null)
        //{
        //    if (!string.IsNullOrEmpty(action.characterLanguage.languageContent))
        //    {
        //        //yield return 通用方法.Instance.下载声音并播放(action.data.characterLanguage.characterVoice, 人物语言音效);

        //        StartCoroutine(播放人物声音(人物语言_Clip, musicStartTime, action));
        //        //人物语言音效 = FindObjectOfType<人物语言>().公用音频_Aud;
        //    }
        //}


        Debug.Log("当前播放的动画为：" + sourceClip.gameObject.name + "从" + startTime + "秒开始" + "从" + startFrame + "帧开始" + Frame / sourceClip[aniName].clip.frameRate + "秒");
        float time = Frame / sourceClip[aniName].clip.frameRate;
        yield return new WaitForSeconds(time);   //要播放的总帧数/帧率就是时间
        //yield return new WaitUntil(() => 声音播放完毕);
        //Debug.Log("我播放到第" + sourceClip[aniName].normalizedTime + "秒了");
        //sourceClip[aniName].normalizedSpeed = 0;
         sourceClip[aniName].speed = 0;
        //if (!setLoop)
        //{
        //    sourceClip[aniName].speed = 0;

        //}
        //else
        //{
        //    sourceClip[aniName].speed = playSpeed;
        //    //StartCoroutine(playLoopAni(sourceClip, startTime, time));
        //    //循环动画Action = playLoopAni(sourceClip, startTime, time, 交互点音乐, musicStartTime);
        //    循环动画对象[sourceClip.gameObject] = playLoopAni(sourceClip, startTime, time, 交互点音乐, musicStartTime);
        //    StartCoroutine(循环动画对象[sourceClip.gameObject]);
        //}
        //print("声音还在播放");
        ////yield return new WaitUntil(() => 声音播放完毕);
        //print("声音播放完毕");

    }

}
